/**
 * 业务对象维护 - 编辑页面
 * @author miaoyl
 * @date 2018/01/04
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        vueData = {
            isAdd: false,
            form: {
                mx_list:[]
            },
            rules: {
                CUSTOM_OBJ: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                CUSTOM_OBJ_NAME_CN: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                CUSTOM_OBJ_NAME_TW: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                CUSTOM_OBJ_NAME_EN: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            }
        },
        edit_table_where = {
            custom_obj:''
        },
        cols = [[
            {type:'checkbox'},
            {field: 'CUSTOM_COLUMN', width: 120, title: '栏位编码',edit: 'text'}
            , {field: 'CUSTOM_COLUMN_NAME_CN', width: 120, title: '栏位中文名称',edit: 'text'}
            , {field: 'CUSTOM_COLUMN_NAME_TW', width: 120, title: '栏位繁体名称',edit: 'text'}
            , {field: 'CUSTOM_COLUMN_NAME_EN',  width: 200, title: '栏位英文名称',edit: 'text'}
            , {field: 'CUSTOM_LEN', width: 170, title: '区域名称',edit: 'text'}
            , {field: 'CUSTOM_FN', width: 200, title: '渲染函数',edit: 'text'}
            , {field: 'CUSTOM_SORT', width: 200, title: '是否允许排序'}
            , {field: 'CUSTOM_DEFAULT', width: 200, title: '默认值',edit: 'text'}
            , {field: 'CUSTOM_REQUIRED', width: 200, title: '必要字段'}
            , {field: 'CUSTOM_VISIBLE', width: 200, title: '显示否'}
            , {field: 'CUSTOM_EXPORT', width: 200, title: '导出Excel否'}
        ]],
        callObj,
        tableData=[],
        checkBoxType='one',
        selectData = []
    ;

    var CustomsEdit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'customs_edit',
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'edit_table'),
                    /*url: g_ctxPath + 'sys/businessObject/getCustomsEdit',
                    method: 'post',*/
                    data:tableData,
                    id: 'edit_table',
                    // toolbar: '#table-toolbar',
                    // defaultToolbar: ['filter', 'exports'],
                    height: 'full-10',
                    cols: cols,
                    loading: false,
                    page: false,
                    where: edit_table_where,
                    done: function (res, curr, count) {
                        tableData = res.data;
                    }
                });

                var tipsIndex=null;

                table.on('checkbox(edit_table)', function(obj){
                    // console.log(obj.checked); //当前是否选中状态
                    // console.log(obj.data); //选中行的相关数据
                    // console.log(obj.type); //如果触发的是全选，则为：all，如果触发的是单选，则为：one
                    //console.log(obj.tr.attr("data-index"));
                    if(obj.checked == true){
                        if(obj.type == 'all'){
                            checkBoxType = 'all';
                        }else {
                            selectData.push(obj);
                        }
                    }else {
                        var index = selectData.indexOf(obj);
                        selectData.splice(index,1);
                    }
                });

                $(doc).on('click', function(){
                    tipsIndex&&layer.close(tipsIndex);
                });

            });
        },

        editCustoms: function () {
            app.ajax({
                xurl: 'sys/businessObject/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: '业务对象修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        callObj.update(result.xdata);
                        layer.close(layer.index);
                    }
                }
            });
        },

        addCustoms: function () {
            app.ajax({
                xurl: 'sys/businessObject/add',
                xdata: vueData.form,
                loading: true,
                okmsg: '业务对象保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('customs').reloadTableCurr();
                        layer.close(layer.index);
                    }
                }
            });
        },
        _call: function (obj) {

            tableData.removeAll();

            if (obj.is_add) {
                vueData.isAdd = true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {},
                    reset: true
                });
                self._reloadTable({id:'edit_table',data : tableData});
            } else {
                vueData.isAdd = false;
                callObj = obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
                edit_table_where.custom_obj = obj.data.CUSTOM_OBJ;
                app.ajax({
                    xurl: 'sys/businessObject/getCustomsEdit',
                    xdata: edit_table_where,
                    loading: true,
                    cb: function (result) {
                        if(result.ok){
                            tableData.pushArray(result.data.data);
                            self._reloadTable({id:'edit_table',data : tableData});
                        }
                    }
                });
            }
        },
        _ready: function () {
            vueData.form = app.fields_(['sys_customs']);
            new Vue({
                el: '#customs_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        layer.close(layer.index);
                    },
                    addData:function () {
                        var data1={CUSTOM_COLUMN:"",CUSTOM_COLUMN_NAME_CN:"",CUSTOM_COLUMN_NAME_TW:"",
                            CUSTOM_COLUMN_NAME_EN:"",CUSTOM_LEN:"",CUSTOM_FN:"",CUSTOM_SORT:"Y",
                            CUSTOM_DEFAULT:"",CUSTOM_REQUIRED:"",CUSTOM_VISIBLE:"Y",CUSTOM_EXPORT:"Y",
                        };
                        tableData.unshift(data1);
                        self._reloadTable({id:'edit_table',data : tableData});
                    },
                    delData:function () {
                        if(checkBoxType == 'all'){
                            tableData = [];
                        }else {
                            for(var i=0;i<selectData.length;i++){
                                selectData[i].del();
                            }
                        }
                        //self._reloadTable({id:'edit_table',data : tableData});
                        selectData = [];
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                var mix = [];
                                for (var j = 0; j <= tableData.length - 1; j++) {
                                    if(tableData[j] != ''){
                                        mix.push({
                                            CUSTOM_COLUMN: tableData[j]["CUSTOM_COLUMN"],
                                            CUSTOM_COLUMN_NAME_CN: tableData[j]["CUSTOM_COLUMN_NAME_CN"],
                                            CUSTOM_COLUMN_NAME_TW: tableData[j]["CUSTOM_COLUMN_NAME_TW"],
                                            CUSTOM_LEN: tableData[j]["CUSTOM_COLUMN_NAME_EN"],
                                            CUSTOM_FN: tableData[j]["CUSTOM_FN"],
                                            CUSTOM_SORT: tableData[j]["CUSTOM_SORT"],
                                            CUSTOM_DEFAULT: tableData[j]["CUSTOM_DEFAULT"],
                                            CUSTOM_REQUIRED: tableData[j]["CUSTOM_REQUIRED"],
                                            CUSTOM_VISIBLE: tableData[j]["CUSTOM_VISIBLE"],
                                            CUSTOM_EXPORT: tableData[j]["CUSTOM_EXPORT"]
                                        });
                                    }
                                }
                                vueData.form.mx_list = JSON.stringify(mix);
                                if (vueData.isAdd) {
                                    self.addCustoms();
                                } else {
                                    self.editCustoms();
                                }
                            }
                        });
                    }
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('customs_edit');
                }
            });

            // app.ajax({
            //     xurl: 'sys/businessObject/getCustomsEdit',
            //     xdata: edit_table_where,
            //     cb: function (result) {
            //         tableData=result.data.data;
            //         self.initTable();
            //     }
            // });

            self.initTable();

        },
        _lazy: function () {
            this._super();
        }
    });

    return new CustomsEdit();

});